package com.youlu.campus.admin.auth.controller;

import com.youlu.campus.admin.auth.entity.Menu;
import com.youlu.campus.admin.auth.entity.SystemResource;
import com.youlu.campus.admin.auth.service.ResourceService;
import com.youlu.campus.admin.auth.vo.MenuVo;
import com.youlu.campus.admin.auth.vo.SystemResourceVo;
import com.youlu.campus.admin.utils.AuthUtils;
import com.youlu.campus.common.base.RestResult;
import com.youlu.campus.common.base.RestResultBuilder;
import com.youlu.campus.entity.VO.req.SystemResourceQueryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源控制器
 * Created by zhuhuaiqi
 */
@RestController
@RequestMapping("/auth/resource")
public class ResourceRestController {

    @Autowired
    private ResourceService resourceService;

    @GetMapping("/menus/{roleId}")
    public RestResult listMenus(@PathVariable(name = "roleId") @NotNull String roleId) {

        String comId = AuthUtils.getComId();
        List<Menu> menus = resourceService.listMenusByRoleId(roleId, comId);
        if (menus == null) {
            return RestResultBuilder.builder().success().data(new ArrayList<>()).build();
        }
        return RestResultBuilder.builder().success().data(menus).build();
    }

    @RequestMapping("/menus/all")
    public RestResult getAllMenus() {

        String comId = AuthUtils.getComId();
        List<Menu> menus = resourceService.getAllMenus(comId);
        if (menus == null) {
            return RestResultBuilder.builder().success().data(new ArrayList<>()).build();
        }
        return RestResultBuilder.builder().success().data(menus).build();
    }

    @PostMapping("/menu/create")
    public RestResult createMenu(@RequestBody @Valid MenuVo menuVo) {
        String comId = AuthUtils.getComId();
        menuVo.setTenantId(comId);
        boolean result = resourceService.createMenu(menuVo);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/create")
    public RestResult createResource(@RequestBody @Valid SystemResourceVo re) {
        String comId = AuthUtils.getComId();
        re.setTenantId(comId);
        SystemResource r = new SystemResource();
        BeanUtils.copyProperties(re, r);
        boolean result = resourceService.createResource(r);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/update")
    public RestResult updateResource(@RequestBody @Valid SystemResource re) {
        String comId = AuthUtils.getComId();
        re.setTenantId(comId);
        boolean result = resourceService.updateResource(re);
        if (result) {
            return RestResultBuilder.builder().success().build();
        }
        return RestResultBuilder.builder().failure().build();
    }

    @PostMapping("/list")
    public RestResult listResource(@RequestBody @Valid SystemResourceQueryVO re) {
        String comId = AuthUtils.getComId();
        re.setTenantId(comId);
        return RestResultBuilder.builder().success(resourceService.list(re)).build();
    }

    @GetMapping("/find-one")
    public RestResult findOneResource(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        return RestResultBuilder.builder().success(resourceService.findOne(id)).build();
    }
    /**
     * 逻辑删除非物理删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public RestResult deleteResource(@RequestParam("id") String id) {
        String comId = AuthUtils.getComId();
        return RestResultBuilder.builder().success(resourceService.delete(id)).build();
    }
}
